package ch09.d1;

import org.junit.Test;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

public class CollectionTest {
    @Test
    public void testCollection(){
        // 创建集合对象
        Collection<String> c=new ArrayList<String>();
        Collection<String> c2=new ArrayList<String>();
//        泛型
//        Collection<String> c=new HashSet<>();

        // 添加元素
        c.add("孙悟空");
        c.add("猪八戒");
        c.add("沙和尚");
        c.add("唐僧");
        c.add("孙悟空");

//        Collections.shuffle(c);


        Collection<String>  c3=List.of("孙悟空","猪八戒","沙和尚","唐僧","孙悟空");


        //迭代器，取得每一个元素
//        Iterator<String> it=c.iterator();// 获取迭代器对象

//        while (it.hasNext()){// 判断是否有元素
//            String str=it.next();// 获取元素
////            if(str.equals("孙悟空")){
////                it.remove();// 删除元素
////            }
//            System.out.println(str);
//        }
//        System.out.println(c);

//        for (String e : c) {
//            System.out.println(e);
//        }
        String[] a=c.toArray(new String[]{});
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }

//        System.in;
        System.err.println(1222);
        System.err.println("1111");
    }

    @Test
    public void testList(){
        List<String> lst=new ArrayList<>();

        // 添加元素
        lst.add("孙悟空");
        lst.add("猪八戒");
        lst.add("沙和尚");
        lst.add("唐僧");
        lst.add("孙悟空");



        for (int i = 0; i < lst.size(); i++) {
            System.out.println(lst.get(i));
        }

        lst.retainAll(List.of("孙悟空","猪八戒","沙和尚","唐僧","孙悟空"));

//        for (String s : lst) {//迭代器
//            System.out.println(s);
//        }

    }

    @Test
    public void testSet(){
//        Set<String> set=new HashSet<>();
        Set<String> set=new TreeSet<>();

        // 添加元素
        set.add("孙悟空");//true
        set.add("猪八戒");
        set.add("沙和尚");
        set.add("唐僧");
        set.add("孙悟空");//false
        set.add("孙行者");//true
        set.add("孙二娘");

        System.out.println(set);


    }


    @Test
    public void testMap(){
        Map<String,Integer> m=new HashMap<>();

        m.put("孙悟空",100);
        m.put("猪八戒",200);
        m.put("沙和尚",300);
        m.put("唐僧",400);
        m.replace("孙行者",500);


        System.out.println(m);

//        Set<String> keys=m.keySet();
//        for (String key : keys) {
//           Integer value= m.get(key);
//           if(value>200){
//               System.out.println(key+"="+value);
//           }
//
//        }
    }

    @Test
    public void testStudent(){
        //姓名、成绩、电话、地址
        Student s1=new Student("孙悟空",100,"123456","花果山");
        Student s2=new Student("猪八戒",200,"123456","高老庄");

        Set<Student> lst=new HashSet<>();
        lst.add(s1);
        lst.add(s2);
        lst.add(new Student("沙和尚",300,"123456","流沙河"));
        lst.add(new Student("唐僧",400,"123456","长安"));
        lst.add(new Student("白龙马",100,"123456","东海"));
        lst.add(new Student("白龙马",100,"123456","东海"));

//        lst.remove(new Student("白龙马",100,"123456","东海"));
//        lst.remove(s1);
//
//        lst.contains(new Student("白龙马",100,"123456","东海"));

        for (Student s : lst) {
            System.out.println(s);
        }

        StringBuffer buf=new StringBuffer(100);
        List<Map> list=new ArrayList<>(100);
        HashMap m1=new HashMap();
        m1.put("name","孙悟空");
        m1.put("age",100);
        m1.put("address","花果山");

        list.add(m1);
        HashMap m2=new HashMap();
        m2.put("name","猪八戒");
        m2.put("age",200);
        m2.put("address","高老庄");
        list.add(m2);

        System.out.println(list);

        List<List> list2=new ArrayList<>();
        List<Set> list3=new ArrayList<>();
        Map<String ,List<Map>> map=new HashMap<>();


    }




}
